package filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Волков Олег on 29.05.14.
 */
public class NoCasheFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        try {
            if (resp instanceof HttpServletResponse) {
                HttpServletResponse httpresponse = (HttpServletResponse) resp;
                // Set the Cache-Control and Expires header
                httpresponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
                httpresponse.setHeader("Expires", "0");
                // Print out the URL we're filtering
                String name = ((HttpServletRequest) req).getRequestURI();
                System.out.println("No Cache Filtering: " + name);
            }
            chain.doFilter(req, resp);
        } catch (IOException e) {
            System.out.println("IOException in NoCacheFilter");
            e.printStackTrace();
        } catch (ServletException e) {
            System.out.println("ServletException in NoCacheFilter");
            e.printStackTrace();
        }
    }



    @Override
    public void destroy() {

    }
}
